import csv
from concurrent.futures.thread import ThreadPoolExecutor
from time import sleep
import requests


csvfile = open("数据.csv", "a", newline="", encoding="ANSI")
# 创建csv写入对象
writer = csv.writer(csvfile)


def download_one_page(url, data, i):
    header = {
        "Referer": "http://www.xinfadi.com.cn/priceDetail.html",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0"
    }
    resp = requests.post(url, data=data, headers=header)
    # resp.encoding = "utf-8"
    print()
    msg = "msg"
    if resp.status_code == 200 :
        msg += "200"
        list_json = resp.json()["list"]
         # 写入csv表头
        # writer.writerow(list_json[0].keys())
        # 写入数据
        for row in list_json:
                writer.writerow(row.values())
        # csvfile.close()
        # resp.close()
    print("导出完成", i , "-------", msg)




if __name__ == '__main__':
    urlParam = "http://www.xinfadi.com.cn/getPriceData.html"

    with ThreadPoolExecutor(50) as t:
        for a in range(1,200):
            sleep(0.1)
            dataParam = {
                "current": f"{a}",
                "limit": "20",
            }
            t.submit(download_one_page,urlParam, dataParam, a)

